package skyproc;

import java.util.ArrayList;
import java.util.Objects;

/**
 *
 * @author Arkangel
 */
public class COBJ extends MajorRecord {

    private static final Type[] type = {Type.COBJ};

    SubList<SubFormInt> ingredients = new SubList<>(Type.COCT, 4, new SubFormInt(Type.CNTO));
    SubData COED = new SubData(Type.COED);
    SubList<Condition> CONDs = new SubList<>(new Condition());
    SubForm CNAM = new SubForm(Type.CNAM);
    SubForm BNAM = new SubForm(Type.BNAM);
    SubData NAM1 = new SubData(Type.NAM1);

    /**
     *
     */
    public KeywordSet keywords = new KeywordSet();

    COBJ() {
	super();

        subRecords.add(ingredients);
        subRecords.add(COED);
	subRecords.add(CONDs);
        subRecords.add(CNAM);
        subRecords.add(BNAM);
        subRecords.add(NAM1);
	subRecords.add(keywords);
    }

    @Override
    Type[] getTypes() {
	return type;
    }

    @Override
    Record getNew() {
	return new COBJ();
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final COBJ other = (COBJ) obj;
        if (!Objects.equals(this.ingredients, other.ingredients)) {
            return false;
        }
        if (!Objects.equals(this.COED, other.COED)) {
            return false;
        }
        if (!Objects.equals(this.CONDs, other.CONDs)) {
            return false;
        }
        if (!Objects.equals(this.CNAM, other.CNAM)) {
            return false;
        }
        if (!Objects.equals(this.BNAM, other.BNAM)) {
            return false;
        }
        if (!Objects.equals(this.NAM1, other.NAM1)) {
            return false;
        }
        if (!Objects.equals(this.keywords, other.keywords)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 67 * hash + Objects.hashCode(this.ingredients);
        hash = 67 * hash + Objects.hashCode(this.COED);
        hash = 67 * hash + Objects.hashCode(this.CONDs);
        hash = 67 * hash + Objects.hashCode(this.CNAM);
        hash = 67 * hash + Objects.hashCode(this.BNAM);
        hash = 67 * hash + Objects.hashCode(this.NAM1);
        hash = 67 * hash + Objects.hashCode(this.keywords);
        return hash;
    }

    @Override
    public MajorRecord merge(MajorRecord no, MajorRecord bo) {
        super.merge(no, bo);
        COBJ c = this;
        if (!(no == null && bo == null && (no instanceof COBJ) && (bo instanceof COBJ))) {
            final COBJ nc = (COBJ) no;
            final COBJ bc = (COBJ) bo;
            SubRecords sList = c.subRecords;
            SubRecords nsList = nc.subRecords;
            SubRecords bsList = bc.subRecords;
            for (SubRecord s : sList) {
                if (s.equals(c.ingredients)) {
                    c.ingredients.mergeList(nc.ingredients, bc.ingredients);
                } else {
                    s.merge(nsList.get(s.type[0]), bsList.get(s.type[0]));
                }
            }
        }
        return c;
    }

    /**
     *
     * @return
     */
    public ArrayList<Condition> getConditions  () {
	return CONDs.toPublic();
    }

    /**
     *
     * @param c
     */
    public void addCondition (Condition c) {
	CONDs.add(c);
    }

    /**
     *
     * @param c
     */
    public void removeCondition (Condition c) {
	CONDs.remove(c);
    }

    /**
     *
     * @param itemReference
     * @param count
     * @return
     */
    public boolean addIngredient(FormID itemReference, int count) {
	return ingredients.add(new SubFormInt(Type.CNTO, itemReference, count));
    }

    /**
     *
     * @param itemReference
     * @return
     */
    public boolean removeIngredient(FormID itemReference) {
	return ingredients.remove(new SubFormInt(Type.CNTO, itemReference, 1));
    }

    public void clearIngredients() {
	ingredients.clear();
    }

    public ArrayList<SubFormInt> getIngredients() {
	return SubList.subFormIntToPublic(ingredients);
    }

    /**
     *
     * @return
     */
    public FormID getResultFormID() {
        return CNAM.getForm();
    }

    /**
     *
     * @param form
     */
    public void setResultFormID(FormID form) {
        CNAM.setForm(form);
    }

    /**
     *
     * @return
     */
    public FormID getBenchKeywordFormID() {
        return BNAM.getForm();
    }

    /**
     *
     * @param form
     */
    public void setBenchKeywordFormID(FormID form) {
        BNAM.setForm(form);
    }

    /**
     *
     * @return
     */
    public int getOutputQuantity() {
        return NAM1.toInt();
    }

    /**
     *
     * @param n
     */
    public void setOutputQuantity(int n) {
        NAM1.setData(n);
    }

}
